var table;
var $;
var layer;
var form;
layui.use(['table','jquery','layer','form'],function () {
    table = layui.table;
    $ = layui.jquery;
    layer = layui.layer;
    form = layui.form;

    var headTable = table.render({
        elem:"#thead",
        url:'/comment/microcircle/getFrontEndCommentMicrocircleCount',
        size: 'lg',
        cols:[[
            {field:'commentSum',title:'历史评论次数',align:'center'},
            {field:'commentPeopleSum',title:'历史评论人数',align:'center'},
            {field:'commentTodaySum',title:'今日评论次数',align:'center'},
            {field:'commentTodayPeopleSum',title:'今日评论人数',align:'center'},
        ]],
        done:function(res) {
            $('#all').children('span').text(res.data[0].all);
            $('#normal').children('span').text(res.data[0].normal);
            $('#intercept').children('span').text(res.data[0].intercept);
            $('#remove').children('span').text(res.data[0].remove);
        }
    })

    var cols ;

    var commentTable = table.render({
        elem:"#root",
        url:"/comment/microcircle/getFrontEndCommentMicrocircle",
        toolbar:'#toptool',
        defaultToolbar: ['filter', 'exports', 'print'],
        limit: 10,
        page: {
            layout: ['count', 'prev', 'page', 'next', 'skip', 'refresh'],
            first: "首页",
            last: "尾页",
        },
        size: 'lg',
        cols:[[
            {type:'checkbox',align:'center',width:60},
            {type:'numbers',title:'序号',align:'center',width:60,templet:function (data) {
                    return data.LAY_INDEX;
                }},
            {field:'userName',title:'成员账号',align:'center'},
            {field:'productName',title:'所属微圈',align:'center'},
            {field:'commentDate',title:'发言时间',align:'center'},
            {field:'content',title:'发言内容',align:'center'},
            {field:'display',title:'状态',align:'center',templet:function (data) {
                    if(data.display == '2'){
                        return '显示';
                    }else if(data.display == '3'){
                        return '不显示';
                    }
                }},
            {title:'操作',align:'center',templet:function (data) {
                    var tem = '<a  href="javascript:getCommentMicrocircleById('+data.commodityId+');" style="color:rgb(13,171,158);margin-right: 20px;">查看</a>'+
                        '<a  href="javascript:interceptCommentMicrocircleById('+data.id+');" style="color:rgb(13,171,158);margin-right: 20px;">拦截</a>'+
                        '<a  href="javascript:removeCommentMicrocircleById('+data.id+');" style="color:rgb(13,171,158)">删除</a>';
                    return tem;
                }},

        ]],
    })

    $('.layui-card-body').children('button').click(function () {
        $(this).removeClass('layui-btn-primary').siblings().addClass('layui-btn-primary');
        if ($(this).text() == '前端用户'){
            headTable.reload({
                url:'/comment/microcircle/getFrontEndCommentMicrocircleCount'
            })
            commentTable.reload({
                url:'/comment/microcircle/getFrontEndCommentMicrocircle'
            })
        } else if($(this).text() == '管理人员'){
            headTable.reload({
                url:'/comment/microcircle/getBackstageCommentMicrocircleCount'
            })
            commentTable.reload({
                url:'/comment/microcircle/getBackstageCommentMicrocircle'
            })
        }
    })

    $('.layui-btn-container').children('button').click(function () {
        $(this).removeClass('layui-btn-primary').siblings().addClass('layui-btn-primary');
        if ($(this).text() == $('#all').text()){
            headTable.reload({
                url:'/comment/microcircle/getFrontEndCommentMicrocircleCount'
            })
            getAround(null);
        }else if($(this).text() == $('#normal').text()){
            headTable.reload({
                url:'/comment/microcircle/getFrontEndCommentMicrocircleCount?status=1'
            })
            getAround('1');
        }else if($(this).text() == $('#intercept').text()){
            headTable.reload({
                url:'/comment/microcircle/getFrontEndCommentMicrocircleCount?status=2'
            })
            getAround('2');
        }else if($(this).text() == $('#remove').text()){
            headTable.reload({
                url:'/comment/microcircle/getFrontEndCommentMicrocircleCount?status=3'
            })
            getAround('3');
        }
    })

    function getAround(status){
        if ($('#frontEnd').hasClass('layui-btn-primary')){
            if(status == '1' || status == null){
                getNormalCols('/comment/microcircle/getBackstageCommentMicrocircle?status='+status)

            }else if(status == '2'){
                getInteceptCols('/comment/microcircle/getBackstageCommentMicrocircle?status='+status)

            }else if(status == '3'){
                getRemoveCols('/comment/microcircle/getBackstageCommentMicrocircle?status='+status)
            }
        } else if($('#backstage').hasClass('layui-btn-primary')){
            if(status == '1'  || status == null){
                getNormalCols('/comment/microcircle/getFrontEndCommentMicrocircle?status='+status)

            }else if(status == '2'){
                getInteceptCols('/comment/microcircle/getFrontEndCommentMicrocircle?status='+status)

            }else if(status == '3'){
                getRemoveCols('/comment/microcircle/getFrontEndCommentMicrocircle?status='+status)
            }
        }
    }

    function getNormalCols(url){
        cols = [[
            {type:'checkbox',align:'center',width:60},
            {type:'numbers',title:'序号',align:'center',width:60,templet:function (data) {
                    return data.LAY_INDEX;
                }},
            {field:'userName',title:'成员账号',align:'center'},
            {field:'productName',title:'所属微圈',align:'center'},
            {field:'commentDate',title:'发言时间',align:'center'},
            {field:'content',title:'发言内容',align:'center'},
            {field:'display',title:'状态',align:'center',templet:function (data) {
                    if(data.display == '2'){
                        return '显示';
                    }else if(data.display == '3'){
                        return '不显示';
                    }
                }},
            {title:'操作',align:'center',templet:function (data) {
                    var tem = '<a  href="javascript:getCommentMicrocircleById('+data.commodityId+');" style="color:rgb(13,171,158);margin-right: 20px;">查看</a>'+
                        '<a  href="javascript:interceptCommentMicrocircleById('+data.id+');" style="color:rgb(13,171,158);margin-right: 20px;">拦截</a>'+
                        '<a  href="javascript:removeCommentMicrocircleById('+data.id+');" style="color:rgb(13,171,158)">删除</a>';
                    return tem;
                }},
        ]];

        commentTable.reload({
            url:url,
            cols:cols,
        })
    }

    function getInteceptCols(url){
        cols = [[
            {type:'checkbox',align:'center',width:60},
            {type:'numbers',title:'序号',align:'center',width:60,templet:function (data) {
                    return data.LAY_INDEX;
                }},
            {field:'userName',title:'成员账号',align:'center'},
            {field:'productName',title:'所属微圈',align:'center'},
            {field:'commentDate',title:'发言时间',align:'center'},
            {field:'content',title:'发言内容',align:'center'},
            {field:'display',title:'状态',align:'center',templet:function (data) {
                    if(data.display == '2'){
                        return '显示';
                    }else if(data.display == '3'){
                        return '不显示';
                    }
                }},
            {title:'操作',align:'center',templet:function (data) {
                    var tem = '<a href="javascript:removeCommentMicrocircleById('+data.id+');" style="color:rgb(13,171,158)">删除</a>';
                    return tem;
                }},
        ]];

        commentTable.reload({
            url:url,
            cols:cols,
        })
    }

    function getRemoveCols(url){
        cols = [[
            {type:'checkbox',align:'center',width:60},
            {type:'numbers',title:'序号',align:'center',width:60,templet:function (data) {
                    return data.LAY_INDEX;
                }},
            {field:'userName',title:'成员账号',align:'center'},
            {field:'productName',title:'所属微圈',align:'center'},
            {field:'commentDate',title:'发言时间',align:'center'},
            {field:'content',title:'发言内容',align:'center'},
            {field:'display',title:'状态',align:'center',templet:function (data) {
                    if(data.display == '2'){
                        return '显示';
                    }else if(data.display == '3'){
                        return '不显示';
                    }
                }},
        ]];

        commentTable.reload({
            url:url,
            cols:cols,
        })
    }

    $('#reset').click(function () {
        location.reload();
    })

    back = function back(){
        parent.$('#beforeHref').val($('.layui-elem-quote').children('span').eq(0).text());
        parent.before();
        return false;
    }

    getCommentMicrocircleById = function getCommentMicrocircleById(id) {
        var obj;
        var index = layer.open({
            type:2,
            title:'查看',
            content:'comment-microcircle-get.html?id='+id+'&userId='+parent.$('#userId').val()+'&userUrl='+parent.$('#userUrl').val(),
            maxmin:true,
            success:function(layero,index){
                obj = layero.find('iframe').contents();
            },
        })
        layer.full(index);
    }

    interceptCommentMicrocircleById = function interceptCommentMicrocircleById(id) {
        layer.confirm('是否确定拦截此条评论', {
                icon: 3, title: '提示'
            },
            function (index) {
                //通过id删除表格数据
                $.ajax({
                    url: "/comment/microcircle/editCommentMicrocircleStatus",
                    method: "post",
                    dataType: "json",
                    data: {id: id,status:'拦截'},
                    success: function (data) {
                        if (data.data > 0) {
                            layer.msg("拦截成功");
                            layer.close(index);
                            $('.layui-card-body').children('button').each(function () {
                                if(!$(this).hasClass('layui-btn-primary')){
                                    $(this).click();
                                }
                            })
                        } else {
                            layer.msg("拦截失败");
                            layer.close(index);
                        }
                    }
                })

            }
        )
    }

    removeCommentMicrocircleById = function removeCommentMicrocircleById(id) {
        layer.confirm('是否确定删除此条评论', {
                icon: 3, title: '提示'
            },
            function (index) {
                //通过id删除表格数据
                $.ajax({
                    url: "/comment/microcircle/editCommentMicrocircleStatus",
                    method: "post",
                    dataType: "json",
                    data: {id: id,status:'删除'},
                    success: function (data) {
                        if (data.data > 0) {
                            layer.msg("删除成功");
                            layer.close(index);
                            $('.layui-card-body').children('button').each(function () {
                                if(!$(this).hasClass('layui-btn-primary')){
                                    $(this).click();
                                }
                            })
                        } else {
                            layer.msg("删除失败");
                            layer.close(index);
                        }
                    }
                })

            }
        )
    }


    back = function back() {
        //通过页面找iframe
        parent.$('#beforeHref').val($('.layui-elem-quote').children('span').eq(0).text());
        parent.before();
        return false;
    }


})